package com.ltl.project.upload_server.dao.impl;

import com.ltl.project.upload_server.dao.FileInfoDAO;
import com.ltl.project.upload_server.pojo.FileInfo;
import com.ltl.project.upload_server.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

/**
 * DAO接口的实现类
 *
 * @author AUSA
 */
public class FileInfoDAOImpl extends BaseDAO<FileInfo> implements FileInfoDAO {

    @Override
    public int insertOneFileInfo(FileInfo fileInfo) {

        Connection conn = JDBCUtils.getConnection();
        String sql = "insert into file_info(file_ori_name,file_size,file_type,create_time,save_path,envelope,uuid) values(?,?,?,?,?,?,?)";

        int rows = update(conn,
                sql,
                fileInfo.getFileOriName(),
                fileInfo.getFileSize(),
                fileInfo.getFileType(),
                fileInfo.getCreateTime(),
                fileInfo.getSavePath(),
                fileInfo.getEnvelope(),
                fileInfo.getUuid());

        try {
            conn.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return rows;
    }

    @Override
    public FileInfo selectOneFileInfoByUuid(String uuid) {

        Connection conn = JDBCUtils.getConnection();
        String sql = "select * from file_info where uuid = ?";
        FileInfo fileInfo = getOne(conn, sql, uuid);
        return fileInfo;
    }

    @Override
    public List<FileInfo> selectLastTenFileInfo() {

        Connection conn = JDBCUtils.getConnection();
        String sql = "select * from file_info order by ID desc offset 0 rows fetch next 10 rows only";
        List<FileInfo> list = getList(conn, sql, null);
        return list;
    }
}
